<?php
    if($_SESSION['home']=="new")
    {
      # proverjajem, ne bilo li zaprosa na novuju igru
      if(isset($_POST['game_submit']))
      {
        $sql_new = "insert into tt_games (type,sts,name,owner,year,ccount,ucount)
                    values(".mysql_real_escape_string($_POST['game_type']).",
                           0,
                           'tt',
                           ".$_SESSION['userid'].",
                           0,
                           ".mysql_real_escape_string($_POST['game_ccount']).",
                           ".mysql_real_escape_string($_POST['game_ucount'])."
                            );";
        $tt_result = mysql_query($sql_new);
        print mysql_error(); 
        $sql_new = "insert into tt_games_herds (game,user,npp,sts,nik,Z,V,TT,PZ,B,PV,E,PT,TZ)
                    values(".mysql_insert_id().",
                           ".$_SESSION['userid'].",
                           1,
                           1,
                           '".mysql_real_escape_string($_POST['game_nik'])."',
                           '100000',
                           '2000',
                           '0',
                           '10',
                           '20000',
                           '-20',
                           '25',
                           '0',
                           '0.1'
                           );";
        $tt_result = mysql_query($sql_new);
        print mysql_error(); 
        $_SESSION['home'] = "games";
        header('Location: index.php');
        exit();
      }

      print "<BR>LIST OF GAMES USER CAN JOIN: <BR>\n";
      $sql_games = "select tt_games.* 
                    from tt_games 
                    where tt_games.sts = 0";
      $tt_games = mysql_query($sql_games);
      print mysql_error(); 
      if(mysql_num_rows($tt_games)>0)
      {
        $flag = 0;
        while($tt_game = mysql_fetch_array($tt_games))
        {
          # proverjajem, jest li mesto v igre
          $sql_place = "select * from tt_games_herds where tt_games_herds.game = ".$tt_game['id'];
          $tt_result = mysql_query($sql_place);
          print mysql_error(); 
          if(mysql_num_rows($tt_result)==$tt_game['ucount'])
          {
            # vse mesta zanjati
            continue;
          }  
          # proverjajem ne podsojedenilsa li uzhe ja sam
          $sql_place = "select * from tt_games_herds where tt_games_herds.game = ".$tt_game['id']." and tt_games_herds.user = ".$_SESSION['userid'];
          $tt_result = mysql_query($sql_place);
          print mysql_error(); 
          if(mysql_num_rows($tt_result)>0)
          {
            # a ja uzhe tuta
            continue;
          }  
          
          if($flag==0)
          {
            print "<TABLE BORDER='1'>
                   <TR>
                     <TD>ID</TD>
                     <TD>TYPE</TD>
                     <TD>NAME</TD>
                     <TD>STATUS</TD>
                     <TD>YEAR</TD>
                     <TD>USERS</TD>
                   <TR>";
            $flag = 1;
          }

          $gusers = "";
          $sql_users = "select tt_games_herds.nik 
                        from tt_games_herds 
                        where tt_games_herds.game = ".$tt_game['id'];
          $tt_users = mysql_query($sql_users);
          while($tt_user = mysql_fetch_array($tt_users))
          {
            if($gusers!="")
            { 
              $gusers = $gusers.",";
            }
            $gusers = $gusers.$tt_user['nik'];
          }
          if($gusers=="")
          {
            $gusers = "no users";
          }
          print "  <TR>
                     <TD>".$tt_game['id']."</TD>
                     <TD>".$tt_game['type']."</TD>
                     <TD>".$tt_game['name']."</TD>
                     <TD>".$tt_game['sts']."</TD>
                     <TD>".$tt_game['year']."</TD>
                     <TD>".$gusers."</TD>
                   <TR>";
        }
        print "</TABLE>";
      }
      else
      {
        # pishem, chto netu igr
        print " no games user can join :( <BR>\n";
      }
      print "<BR>BUT YOU CAN STILL CREATE NEW ONE:";
      print "<FORM NAME='NEW_GAME' METHOD='POST' ACTION='index.php'>
             TYPE OF GAME:<SELECT NAME='game_type'>
                            <OPTION SELECTED VALUE='0'>normal</OPTION>
                            <OPTION VALUE='1'>duel</OPTION>
                          </SELECT><BR>
             NUMBER OF USERS:<SELECT NAME='game_ucount'>
                               <OPTION SELECTED VALUE='2'>2</OPTION>
                               <OPTION VALUE='3'>3</OPTION>
                               <OPTION VALUE='4'>4</OPTION>
                               <OPTION VALUE='5'>5</OPTION>
                               <OPTION VALUE='6'>6</OPTION>
                               <OPTION VALUE='7'>7</OPTION>
                               <OPTION VALUE='8'>8</OPTION>
                             </SELECT><BR>
             NUMBER OF CARDS:<SELECT NAME='game_ccount'>
                               <OPTION VALUE='5'>5</OPTION>
                               <OPTION SELECTED VALUE='7'>7</OPTION>
                             </SELECT><BR>
             NICKNAME:<INPUT NAME='game_nik' TYPE='TEXT' VALUE='".$_SESSION['login']."'><BR>
             <INPUT NAME='game_submit' TYPE='SUBMIT' VALUE='create game'>
             </FORM>";
    }
?>
